var defaultParameter = {
  pageNumber: 1,
  pageSize: 20,
  option:'stats'
};

var filterParam={

};

function initTable(){

  var columns=[[
    {field:'impleProjId',title:'项目id',fitColumns:true,hidden:true,
    },
    {field:'impleProjName',title:'实施项目名称',width:$(this).width()*0.1,fitColumns:true,
    },
    {field:'projStateName',title:'阶段',width:$(this).width()*0.1,fitColumns:true,
        formatter: function(value,row,index){
            if(row.projectStage!=null){
                return row.projectStage.projStateName;
            }else{
                return "";
            }
        }
     },
    {field:'uploadName',title:"文件名称",width:$(this).width()*0.1,fitColumns:true,
         formatter: function(value,row,index){
          if(row.id!='static'){
           if(row.docInfo!=null){
             return row.docInfo.fileName;
           }else{
             return "";
           }
           }else{
             return "文件总数:"+row.docInfoTotal;
           }


         }
        },
    {field:'fileAttr',title:'文档属性',width:$(this).width()*0.1,fitColumns:true,
          formatter: function(value,row,index){
            if(row.uploadInfos!=null && row.uploadInfos.length>0) {
              return row.uploadInfos[0].fileAttrName;
            }else{
              return "";
            }
          }
    },
   {field:'fileType',title:'文档类型',width:$(this).width()*0.1,fitColumns:true,
              formatter: function(value,row,index){
                if(row.uploadInfos!=null && row.uploadInfos.length>0) {
                  return row.uploadInfos[0].fileTypeName;
                }else{
                  return "";
                }
              }
   },

    {field:'statusCdName',title:'状态',width:$(this).width()*0.1,fitColumns:true,
      formatter: function(value,row,index){
       if(row.id!='static'){
         return row.docInfo.statusCdName;
       }else{
        return "借出文档数:"+row.borrowedTotal;
       }
      }
    },
    {field:'participant',title:'参与人',width:$(this).width()*0.1,fitColumns:true,
      formatter: function(value,row) {
     if(row.id!='static'){
        if(row.docInfo!=null) {
          return row.docInfo.participantName;
        }else{
          return "";
        }
      }else{
        return "关键文件数："+row.keyFileTotal;
      }
      }
    },
    {field:'broker',title:"经手人",width:$(this).width()*0.1,fitColumns:true,
      formatter: function(value,row) {
     if(row.id!='static'){
        if(row.docInfo!=null) {
          return row.docInfo.brokerName;
        }else{
          return "";
        }
        }else{
       return "一般文件数："+row.nomFileTotal;
        }
      }
    },
    {field:'borrower',title:"借阅人",width:$(this).width()*0.1,fitColumns:true,
      formatter: function(value,row) {
        if(row.fileBorrow!=null) {
          return row.fileBorrow.borrowerName;
        }else{
          return "";
        }
      }
    },
    {field:'borrowDt',title:"借阅时间",width:$(this).width()*0.1,fitColumns:true,
         formatter: function(value,row) {
            if(row.fileBorrow!=null) {
              return row.fileBorrow.borrowDt;
            }else{
              return "";
            }
          }
    },
    {field:'returnDt',title:"归还时间",width:$(this).width()*0.1,fitColumns:true,
             formatter: function(value,row) {
                if(row.fileBorrow!=null) {
                  return row.fileBorrow.returnDt;
                }else{
                  return "";
                }
              }
    },
    {field:'description',title:"备注",width:$(this).width()*0.1,fitColumns:true,
    },
  ]];

  $('#fileInfoList').datagrid({
    url:'/InfoManageWeb/folderManageService/queryProjectStageDocInfo',
    method:'post',
    dataType : 'json',
    // idField:'id',
    // treeField:'projStageClass',
    queryParams:defaultParameter,
    nowrap:false,
    fitColumns:true,
    pagination:true,
    columns:columns,
    onBeforeLoad : function(param){
      for (var key in param) {
        if(key=="rows"){
          delete param[key];
        }
      }
    },
    loadFilter:function(data){
      if(data.resultCode!=0){
        toastr.error(data.resultCode);
        return null;
      }else{
        if(data.result.rows!=null){
           var static={
               'id':"static",
               "docInfoTotal":data.result.rows[0].docInfoTotal,
               "borrowedTotal":data.result.rows[0].borrowedTotal,
               "keyFileTotal":data.result.rows[0].keyFileTotal,
               "nomFileTotal":data.result.rows[0].nomFileTotal
           };
           data.result.rows.push(static);
        }
        return data.result;
      }
    },
     //  onBeforeExpand : function(row) {
     //      // 此处就是异步加载地所在
     //      if (row) {
     //          if(row.level=="1"){
     //              filterParam.impleProjId=row.impleProjId;
     //              filterParam.parentId=row.id;
     //              $(this).treegrid('options').queryParams=filterParam;
     //              $(this).treegrid('options').url = '/InfoManageWeb/folderManageService/queryProjectStageForDocInfo'
     //          }
     //          if(row.level=="2"){
     //              filterParam.parentId=row.id;
     //              filterParam.projStageId=row.projStageId;
     //              filterParam.option='stats';
     //              $(this).treegrid('options').queryParams=filterParam;
     //              $(this).treegrid('options').url = '/InfoManageWeb/folderManageService/queryDocInfoByProjStageId'
     //          }
     //      }
     //      return true;
     // }
      onLoadSuccess:function(data){
          mergeCellsByField('fileInfoList','impleProjName',"projStateName");
      }

  });
    var p = $('#fileInfoList').datagrid('getPager');
    $(p).pagination({
      pageSize: 20,//每页显示的记录条数，默认为10
      pageList: [10,20,30],//可以设置每页记录条数的列表
      beforePageText: '第',//页数文本框前显示的汉字
      afterPageText: '页    共 {pages}  页',
      displayMsg: '当前显示 {from} - {to} 条记录 共 {total}  条记录',
      onSelectPage:function(pageNumber, pageSize){
     defaultParameter.pageNumber=pageNumber;
     defaultParameter.pageSize=pageSize;
  defaultParameter.impleProjId=$('#itemOut').val();
  defaultParameter.planProjClass=$('#planProjClass').val();
  defaultParameter.planProjId=$('#planProjectId').val();
    // $('#fileInfoList').treegrid('options').url="/InfoManageWeb/folderManageService/queryImplementProjectForDocInfo";
     $('#fileInfoList').datagrid('reload',defaultParameter);
      }
    });
}

function queryStaticInfo(){
  defaultParameter.impleProjId=$('#itemOut').val();
  defaultParameter.planProjClass=$('#planProjClass').val();
  defaultParameter.planProjId=$('#planProjectId').val();
  // $('#fileInfoList').treegrid('options').url="/InfoManageWeb/folderManageService/queryImplementProjectForDocInfo";
  $('#fileInfoList').datagrid('load',defaultParameter);
}

function mergeCellsByField(tableID, fldList,other) {
    var Arr = fldList.split(",");
    var dg = $('#' + tableID);
    var fldName;
    var RowCount = dg.datagrid("getRows").length;
    var span;
    var PerValue = "";
    var CurValue = "";
    var length = Arr.length - 1;
    for (i = length; i >= 0; i--) {
        fldName = Arr[i];
        PerValue = "";
        span = 1;
        for (row = 0; row <= RowCount; row++) {
            if (row == RowCount) {
                CurValue = "";
            }
            else {
                CurValue = dg.datagrid("getRows")[row][fldName];
            }
            if (PerValue === CurValue) {
                span += 1;
            }
            else {
                var index = row - span;
                dg.datagrid('mergeCells', {
                    index: index,
                    field: fldName,
                    rowspan: span,
                    colspan: null
                });
                var name = other.split(",");
                $.each(name,function (i,item){
                    dg.datagrid('mergeCells', {
                        index: index,
                        field: item,
                        rowspan: span,
                        colspan: null
                    });
                });
                span = 1;
                PerValue = CurValue;
            }
        }
    }
}

$(function() {
  initTable();

//  $("#planProjClass").constantCommpent({
//    elmIdSel : "planProjClass",//需要展示的位置下拉框
//    constParamCode : "PLAN_PROJ_CLASS",//需要加载的常量编码
//    callbackFunc: function (objvalue, objname) {//下拉框值改变时回调
//    }
//  });
//
//  $("#itemBtn").click(function() {
//    $("#itemBtn").itemSearchCommpent({
//      elmIdOut : "itemOut",//组织树单击之后回填的文本框（input）
//      elmIdModal : "itemSearchModal",//用户选择
//      elmIdOrgModal : "unitSearchModal3",//组织查询
//      callbackFunc: function (projectId, projectName) {//组织树单击之后回调函数
//        callBackItemProject(projectId, projectName);
//      }
//    });
//  });
  //所属规划-建设单位-实施项目
  $("#planProjClass").project2unit2itemCommpent({
      elmIdPlanTypeSel : "planProjClass",
      elmIdUnitSel : "",//组织单位下拉元素
      elmIdItemSel : "itemOut",//实施项目下拉元素
      orgType : "",//初始化单位时，单位类型
      callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
    	  queryStaticInfo();
      }
  });

  $("#projectBtn").click(function() {
    $("#projectBtn").projectSearchCommpent({
      elmIdOut : "projectOut",//组织树单击之后回填的文本框（input）
      elmIdModal : "projectSearchModal",//用户选择
      elmIdOrgModal : "unitSearchModal",//组织查询
      callbackFunc: function (projectId, projectName) {//组织树单击之后回调函数
        callBackProjectCondition(projectId, projectName);
      }
    });
  });


  $("#btnSearch").click(function(){
    queryStaticInfo();
  });
});